<?php

namespace App\Service\Invoice\Merchant;


use App\Models\Invoice\InvoiceHeadModel;
use App\Models\Invoice\InvoiceHistoryModel;
use App\Service\Invoice\InvoiceService;
use App\Service\BaseService;
use Exception;

class InvoiceHeaderService extends BaseService
{

    /**
     * 获取门店的默认发票抬头
     * @param int $userId
     * @param int $shopId
     * @return array
     */
    public static function getDefaultShopInfo(int $userId, int $shopId): array
    {
        $info = InvoiceHeadModel::query()
                                ->where('invoiceDefault', '=', InvoiceHeadModel::INVOICEDEFAULT_1)
                                ->where('shopId', '=', $shopId)
                                ->first();

        $show_msg = "ok";
        if (empty($info)) {

            // 执行同步
            list($syncStatus, $show_msg) = InvoiceService::syncInvoiceHeader($shopId, $userId);
            if ($syncStatus) {
                $info = InvoiceHeadModel::query()
                                        ->where('invoiceDefault', '=', InvoiceHeadModel::INVOICEDEFAULT_1)
                                        ->where('shopId', '=', $shopId)
                                        ->first();
            }
        }

        if (!empty($info)) {
            unset($info['createdAt']);
            unset($info['createdByUid']);
            unset($info['updatedAt']);
            unset($info['updatedByUid']);
        }

        return [
            'show_msg' => $show_msg,
            'info'     => $info,
        ];

    }

    /**
     *  数据修改
     * @param array $params
     * @throws Exception
     */
    public static function update(array $params): bool
    {
        $invoiceHeadModel = InvoiceHeadModel::query()
                                            ->where('id', $params['id'])
                                            ->where("shopId", $params["shopId"])
                                            ->first();
        if (null == $invoiceHeadModel) {
            throw new Exception("信息不存在");
        }
        if ($params['headerType'] == InvoiceHistoryModel::HEADER_TYPE_1) {// 个人抬头不保存 只保留企业默认数据--产品确认
            $invoiceHeadModel->headerType  = $params['headerType'];
            $invoiceHeadModel->invoiceType = $params['invoiceType'];

            // 抬头和税号可编辑
            $invoiceHeadModel->invoiceTitle     = $params['invoiceTitle'] ?? '';
            $invoiceHeadModel->companyTaxNumber = $params['companyTaxNumber'] ?? '';
            if (!empty($params['companyRegisterAddress'])) {
                $invoiceHeadModel->companyRegisterAddress = $params['companyRegisterAddress'];
            }
            if (!empty($params['companyRegisterMobile'])) {
                $invoiceHeadModel->companyRegisterMobile = $params['companyRegisterMobile'];
            }
            if (!empty($params['companyDepositBank'])) {
                $invoiceHeadModel->companyDepositBank = $params['companyDepositBank'];
            }
            if (!empty($params['companyDepositBankNum'])) {
                $invoiceHeadModel->companyDepositBankNum = $params['companyDepositBankNum'];
            }
        }
        if (!empty($params["email"])) {
            $invoiceHeadModel->email = $params["email"];
        }
        if (!empty($params["mobile"])) {
            $invoiceHeadModel->mobile = $params["mobile"];
        }
        $invoiceHeadModel->updatedAt    = time();
        $invoiceHeadModel->updatedByUid = $params['userId'];
        return $invoiceHeadModel->save();
    }


}
